package org.videopractice.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;

import org.videopractice.R;
import org.videopractice.databinding.ActivityShowScreenBinding;
import org.videopractice.media.MediaDecode;
import org.videopractice.network.ClientServer;

public class ShowScreenActivity extends FragmentActivity {

    private ActivityShowScreenBinding viewDataBinder;
    private MediaDecode mediaDecode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewDataBinder = DataBindingUtil.setContentView(this, R.layout.activity_show_screen);
        Intent intent = getIntent();
        String ipAddress = intent.getStringExtra("ipAddress");
        ClientServer.SERVER.connect(ipAddress);
        initSurfaceView();
    }

    private void initSurfaceView() {
        SurfaceHolder holder = viewDataBinder.surfaceView.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mediaDecode = new MediaDecode(ShowScreenActivity.this, holder.getSurface());

                DisplayMetrics dm = getResources().getDisplayMetrics();
                int width = dm.widthPixels;
                int height = dm.heightPixels;
                mediaDecode.play(width, height);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
    }

}
